﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Theming;

namespace QDP.Platform.Loader
{
    public partial class App : Application
    {
        internal MasterPanel MasterService;

        public IDictionary<string, string> Configurations { get; private set; }
        private Grid container;
        private Border contentContainer;
        private Border splashScreenContainer;

        public App()
        {
            this.Startup += this.Application_Startup;
            this.UnhandledException += this.Application_UnhandledException;
            InitializeComponent();
        }
        // 应用程序启动
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            D.Debug("QDP 启动.");
            container = new Grid();
            // 初始化内容容器
            contentContainer = new Border();
            container.Children.Add(contentContainer);
            // 初始化闪屏容器
            splashScreenContainer = new Border();
            container.Children.Add(splashScreenContainer);

            base.RootVisual = container;
            MasterService = new MasterPanel();
            // 设置平台主界面宿主服务
            Q.SetMasterManagerHost(MasterService);
            //Q.SetDataManagerHost(MasterService);

            D.Debug("获取宿主参数配置.");
            Configurations = e.InitParams;
            // 加载闪屏
            D.Debug("启动加载器.");
            LoadSplashScreen();
        }
        // 加载闪屏界面
        private void LoadSplashScreen()
        {
            var v = new SplasnScreen();
            v.DownloadComplated += (s, e) =>
            {
                this.LoadMainPage();
                container.Children.Remove(splashScreenContainer);
            };
            splashScreenContainer.Child = v;
        }
        /// <summary>加载平台主界面</summary>
        private void LoadMainPage()
        {
            if (contentContainer.Child is Theme)
                (contentContainer.Child as Theme).Content = MasterService as UIElement;
            else
                contentContainer.Child = MasterService as UIElement;
        }
        public void SetTheme(Theme theme)
        {
            theme.Background = null;
            if (contentContainer.Child == null)
            {
                contentContainer.Child = theme;
            }
            else if (contentContainer.Child is Theme)
            {
                theme.Content = (contentContainer.Child as Theme).Content;
                contentContainer.Child = theme;
            }
            else
            {
                theme.Content = contentContainer.Child;
                contentContainer.Child = theme;
            }
        }
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
            D.Debug("异常:{0}\r\n{1}", e.ExceptionObject.Message,e.ExceptionObject.ToString());
            Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDom(e); });
        }

        private void ReportErrorToDom(ApplicationUnhandledExceptionEventArgs e)
        {
            this.ShowMessage(e.ExceptionObject.Message,e.ExceptionObject.ToString());
        }
    }
}